import request from '../utils/request'
import type { 
  Court, 
  TimeSlot, 
  BookingParams, 
  Order 
} from '../types/api'

class BookingService {
  async getCourts(params?: {
    page?: number
    pageSize?: number
    status?: 'available' | 'maintenance' | 'closed'
  }): Promise<{
    courts: Court[]
    total: number
    hasMore: boolean
  }> {
    const response = await request.get<{
      courts: Court[]
      total: number
      hasMore: boolean
    }>('/courts', params)
    
    return response.data
  }

  async getCourtDetail(courtId: string): Promise<Court> {
    const response = await request.get<Court>(`/courts/${courtId}`)
    return response.data
  }

  async getTimeSlots(courtId: string, date: string): Promise<TimeSlot[]> {
    const response = await request.get<TimeSlot[]>(`/courts/${courtId}/timeslots`, {
      date
    })
    return response.data
  }

  async createBooking(params: BookingParams): Promise<Order> {
    const response = await request.post<Order>('/bookings', params)
    return response.data
  }

  async checkTimeSlotAvailability(
    courtId: string, 
    date: string, 
    timeSlotIds: string[]
  ): Promise<{ 
    available: boolean
    conflictSlots: string[]
    totalPrice: number 
  }> {
    const response = await request.post<{
      available: boolean
      conflictSlots: string[]
      totalPrice: number
    }>('/bookings/check-availability', {
      courtId,
      date,
      timeSlotIds
    })
    
    return response.data
  }
}

export const bookingService = new BookingService()
export default bookingService